package com.tianci.appstore.utils;

/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年6月28日         Root.Lu
 */

import android.os.Handler;
import android.os.Looper;

import com.coocaa.x.framework.utils.LogUtils;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class AsyncTask {
    private static final int MAX_TASK = 3;
    private static ThreadPoolExecutor executor = null;
    private static BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();

    public static synchronized void runOnThread(Runnable r) {
        if (executor == null) {
            executor = new ThreadPoolExecutor(MAX_TASK, MAX_TASK, 999, TimeUnit.DAYS, queue,
                    new ThreadFactory() {
                        @Override
                        public Thread newThread(Runnable r) {
                            // TODO Auto-generated method stub
                            Thread thread = new Thread(r, "AsyncTask" + System.currentTimeMillis());
                            thread.setPriority(Thread.MIN_PRIORITY);
                            thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
                            return thread;
                        }
                    });
        }
        executor.execute(r);
    }

    public static synchronized void runOnThread(Runnable r, boolean maxPriority) {
        if (maxPriority) {
            Thread thread = new Thread(r, "MaxPriorityThread" + System.currentTimeMillis());
            thread.setPriority(Thread.MIN_PRIORITY);
            thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
            thread.start();
        } else
            runOnThread(r);
    }

    private static Handler handler = null;

    public static synchronized void runOnUiThread(Runnable r) {
        if (handler == null)
            handler = new Handler(Looper.getMainLooper());
        handler.post(r);
    }

    private static UncaughtExceptionHandler uncaughtExceptionHandler = new UncaughtExceptionHandler() {
        private String log(Throwable ex) {
            String ret = ex.toString() + "\n";
            StackTraceElement[] es = ex.getStackTrace();
            for (StackTraceElement e : es) {
                String file_line = "    " + e.getClassName() + "." + e.getMethodName() + "("
                        + e.getFileName() + "@" + e.getLineNumber() + ")";
                ret += file_line + "\n";
            }
            return ret;
        }

        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            // TODO Auto-generated method stub
            LogUtils.d("e", thread.getName() + " uncaughtException:\n" + log(ex));
        }
    };
}
